package edu.ucdavis.gwt.gis.client.state;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.logical.shared.AttachEvent.Handler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import edu.ucdavis.gwt.gis.client.extras.CloseButton;

public class SavePopup extends PopupPanel {

	private static SavePopupUiBinder uiBinder = GWT.create(SavePopupUiBinder.class);
	interface SavePopupUiBinder extends UiBinder<Widget, SavePopup> {}

	public static SavePopup INSTANCE = new SavePopup();
	
	@UiField TextBox mapNameInput;
	@UiField HTML saveButton;
	@UiField HTML cancelButton;
	@UiField HTML messagePanel;
	@UiField CloseButton closeButton;
	@UiField HTML warningMessage;
	@UiField VerticalPanel updateCurrentPanel;
	@UiField HTML updateButton;
	
	public SavePopup() {
		setWidget(uiBinder.createAndBindUi(this));
		getElement().getStyle().setZIndex(8010);
		setGlassEnabled(true);
		setGlassStyleName("GwtGisPopup-glasspanel");
		
		warningMessage.setHTML(ClientStateManager.WARNING_MESSAGE);
		
		saveButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				save(mapNameInput.getText());
			}
		});
		
		mapNameInput.addKeyUpHandler(new KeyUpHandler(){
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if( event.getNativeKeyCode() == KeyCodes.KEY_ENTER ) {
					save(mapNameInput.getText());
				}
			}
		});
		
		closeButton.addClickHandler(closeHandler);
		cancelButton.addClickHandler(closeHandler);
		
		Window.addResizeHandler(new ResizeHandler(){
			@Override
			public void onResize(ResizeEvent event) {
				if( isAttached() ) center();	
			}
		});
		
		updateButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				String id = ClientStateManager.getCurrentStateId();
				String name = ClientStateManager.getMapName(id);
				save(name);
				ClientStateManager.removeState(id);
			}
		});
		
		addAttachHandler(new Handler(){
			@Override
			public void onAttachOrDetach(AttachEvent event) {
				if( event.isAttached() ) {
					messagePanel.setHTML("");	
					String id = ClientStateManager.getCurrentStateId();
					String name = ClientStateManager.getMapName(id);
					if( name == null ) {
						updateCurrentPanel.setVisible(false);
					} else if( id.length() == 0 ) {
						updateCurrentPanel.setVisible(false);
					} else {
						updateCurrentPanel.setVisible(true);
						updateButton.setText("Update saved map: "+name);
					}
					mapNameInput.setFocus(true);
					mapNameInput.setText("");
					center();
				}
			}
		});
	}
	
	private void save(String name) {
		if( name.length() == 0 ) {
			messagePanel.setHTML("<div style='padding:10px;color:red;text-align:center'>Please provide a name for the map</div>");
		} else {
			ClientStateManager.saveClientState(name);
			saveSuccess();
		}
	}
	
	private void saveSuccess() {
		messagePanel.setHTML("<div style='padding:10px;color: green;text-align:center'>Success!</div>");
		Timer t = new Timer(){
			@Override
			public void run() {
				hide();
			}
		};
		t.schedule(1000);
	}
	
	

	private ClickHandler closeHandler = new ClickHandler(){
		@Override
		public void onClick(ClickEvent event) {
			hide();
		}
	};

}
